﻿using Dapper.Extensions;
using System;
using System.Web.Mvc;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Models;
using YouWei.Infrastructure.Service;
using YouWei.Op.Common;
using YouWei.Utility;
using YouWei.Utility.Logging;
using System.Linq;
using System.Collections.Generic;
using YouWei.Infrastructure.Repository;

namespace YouWei.Op.Controllers
{
    public class CouponController : BaseController
    {
        private CouponService _service = new CouponService();
        private CouponUserRelaService _serviceRela = new CouponUserRelaService();
        #region 优惠券
        /// <summary>
        /// 优惠券管理列表
        /// </summary>
        /// <returns></returns>
        public ActionResult CouponIndex()
        {
            return View();
        }

        /// <summary>
        /// 优惠券列表数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ContentResult CouponList(CouponSearchModel model)
        {
            int pageIndex = GetPageIndex();
            int pageSize = GetPageSize();
            Page<CouponDto> page = _service.Pages(pageIndex, pageSize, model);
            return ReturnResult(page);
        }


        /// <summary>
        /// 优惠券编辑视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult CouponEidt()
        {
            var id = GetParam<int>("id");
            Coupon entity;
            if (id > 0)
                entity = _service.Single(id);
            else
                entity = new Coupon();
            return View(entity);
        }
        /// <summary>
        /// 添加优惠券
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CouponSave(Coupon model)
        {
            ResultData<int> rt = ResultData<int>.Error("");
            if (string.IsNullOrWhiteSpace(model.CouponCode))
            {
                rt.message = "缺少必要的参数CouponCode";
                return ReturnResult(rt);
            }
            model.CouponID = GetParam<int>("id");
            //edit
            if (model.CouponID > 0)
            {
                var entity = _service.Single(model.CouponID);
                if (entity == null)
                {
                    rt.message = "id不正确，优惠券不存在";
                    return ReturnResult(rt);
                }
                model.CouponType = entity.CouponType;
                model.CreateDate = entity.CreateDate;
                model.UpdateDate = DateTime.Now;
                model.IsActive = entity.IsActive;
                var re = _service.Update(model);
                if (!re)
                {
                    rt.message = "更新失败";
                    return ReturnResult(rt);
                }
                else
                {
                    rt.status = 0;
                    rt.message = "更新成功";
                }
            }
            //add
            else
            {
                model.CouponStatus = 0;
                model.IsActive = 1;
                model.CreateDate = DateTime.Now;
                _service.Insert(model);
                rt.status = 0;
                rt.message = "添加成功";
            }
            return ReturnResult(rt);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CommentDelete()
        {
            ResultData<int> rt = ResultData<int>.Error("");
            var id = GetParam<int>("id");
            if (id <= 0)
            {
                rt.message = "缺少必要的参数";
                return ReturnResult(rt);
            }
            var entity = _service.Single(id);
            if (entity != null)
            {
                entity.IsActive = 0;
                entity.UpdateDate = DateTime.Now;
                var res = _service.Update(entity);
                if (res)
                {
                    rt.status = 0;
                    rt.message = "删除成功";
                }
                else
                {
                    rt.status = -1;
                    rt.message = "删除失败";
                }
            }
            else
            {
                rt.message = "id不正确，内容不存在";
            }
            return ReturnResult(rt);
        }

        #endregion

        #region 优惠券分配给用户
        public ActionResult CouponToUser()
        {
            var couponId = GetParam<int>("couponId");
            ViewBag.CouponId = couponId;
            return View();
        }
        /// <summary>
        /// 保存优惠券分配用户关系
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult RelaSave()
        {
            ResultData<int> rt = ResultData<int>.Error("");
            var userId = GetParam<int>("userId");
            var couponId = GetParam<int>("couponId");
            if (userId <= 0 || couponId <= 0)
            {
                rt.message = "缺少必要的参数";
                return ReturnResult(rt);
            }
            //查询用户30天内是否有优惠券信息，有的话则返回
            var isExist = _serviceRela.GetActive(userId);
            if (isExist != null)
            {
                rt.message =$"该用户存在未过期优惠券，id为{isExist.CouponID}，无法继续分配";
                return ReturnResult(rt);
            }
            var coupon = _service.Single(couponId);
            if (coupon != null)
            {
                //增加用户和优惠券关系
                var rela = new CouponUserRela()
                {
                    CardUserId = userId,
                    CouponID = couponId,
                    CreateDate = DateTime.Now,
                    ExpirationDate = DateTime.Now.LastDayOfMonth(),
                    UsedStoreID = 0,//默认为0
                    UseStatus = 0
                };
                _serviceRela.Insert(rela);
                //更新优惠券状态
                coupon.CouponStatus = 1;
                coupon.UpdateDate = DateTime.Now;
                var res = _service.Update(coupon);
                rt.status = 0;
                rt.message = "已成功分配优惠券";
            }
            else
            {
                rt.message = "优惠券不存在";
            }
            return ReturnResult(rt);
        }
        #endregion
    }
}